package fm.dongman.modellib.remote;

import android.support.annotation.NonNull;

import org.json.JSONObject;

import fm.dongman.animefm.contractlib.BaseDataSource;
import fm.dongman.animefm.contractlib.contract.FArticleContract;
import fm.dongman.animefm.contractlib.model.FArticleDataModel;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;

/**
 * 漫圈网络数据加载
 * Created by KUIGE on 2017/10/16.
 */

public class FArticleRemoteDataSource implements FArticleContract.IFArticleDataSource {
    private static FArticleRemoteDataSource SINGLETON;
    private RemoteOperationHelper mRemote;

    private boolean isRemote = true;
    private FArticleRemoteDataSource() {
        mRemote = RemoteOperationHelper.getOperationHelper();
    }

    public static FArticleRemoteDataSource getInstance() {
        if (SINGLETON == null) {
            SINGLETON = new FArticleRemoteDataSource();
        }
        return SINGLETON;
    }

    @Override
    public void getData(@NonNull BaseDataSource.LoadSourceCallback<? super FArticleDataModel> callback) {
        if (isRemote) {
            JSONObject json = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.FIND_ARTICLE_REMOTE + "page=1&size=5&hot_num=5&banner_num=5");
            if (json != null) {
                FArticleDataModel model = mRemote.downFArticle(json);
                callback.onDataLoaded(model);
            } else {
                callback.onDataLoadFiled();
            }
        }else callback.onDataLoadFiled();
    }

    @Override
    public void saveData(FArticleDataModel dataModel) {
    }

    @Override
    public void refreshData() {
    }

    @Override
    public void offLine() {
        isRemote = !isRemote;
    }
}
